package 二叉树;

import java.util.LinkedList;
import java.util.Queue;

public class Solution104 {
    public static void main(String[] args) {
        TreeNode treeNode = new TreeNode(1);
        TreeNode treeNode1 = new TreeNode(2);
        TreeNode treeNode2 = new TreeNode(3);
        treeNode.left = treeNode1;
        treeNode.right = treeNode2;
        System.out.println(maxDepth(treeNode));
    }

    public static int maxDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        int max = 0;
        while (!queue.isEmpty()) {
            int size = queue.size();
            max++;
            while (size > 0) {
                TreeNode poll = queue.poll();
                if (poll != null) {
                    if (poll.left != null) {
                        queue.offer(poll.left);
                    }
                    if (poll.right != null) {
                        queue.offer(poll.right);
                    }
                }
                size--;
            }
        }
        return max;
//        return getLength(root, 0);
    }

    public static int getLength(TreeNode root, int i) {
        if (root == null) {
            return i;
        } else {
            i++;
        }
        int leftMax = getLength(root.left, i);
        int rightMax = getLength(root.right, i);
        return Math.max(leftMax, rightMax);
    }
}
